﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;

namespace ReBiaoDTU
{
    public class ReadReBiao
    {
        public static object sync = new object();
        public bool GetData(string id, List<string> addr, out List<ReBiaoData> dataList)
        {
            dataList=new List<ReBiaoData>();
            if (!ReBiaoPick.bServerStart)
                return false;
            byte[] tx=new byte[5]{0x10 ,0x5b ,0xfe ,0x59 ,0x16};            
            try
            {
                for (int i = 0; i < addr.Count; i++)
                {
                    GPRS_DATA_RECORD recd=new GPRS_DATA_RECORD();
                    List<GPRS_DATA_RECORD> rxList0 = (List<GPRS_DATA_RECORD>)ReBiaoPick.rxDataTable[id];
                    List<GPRS_DATA_RECORD> old_rxList=null;
                    if (rxList0 != null)
                    {
                        old_rxList = new List<GPRS_DATA_RECORD>();
                        for (int k = 0; k < rxList0.Count; k++)
                        {
                            old_rxList.Add(rxList0[k]);
                        }
                    }
                    StringBuilder mess = new StringBuilder(500);
                    if (HongDianDTU.do_send_user_data(id,tx, 5, mess) == 0)
                    {
                        ReBiaoPick.f.ExternShowRx("成功向" + id + "发送数据:" + stringfunc.byteToHexStr(tx, " "));
                    }
                    else
                    {
                        ReBiaoPick.f.ExternShowRx("~~~~~向" + id + "发送数据:" + stringfunc.byteToHexStr(tx, " ")+"发生错误:"+mess.ToString());
                        return false;
                    }
                    int wait = 60;
                    while (wait != 0)
                    {
                        bool bFind = false;
                        bool bRepeat = false;
                        List<GPRS_DATA_RECORD> rxList = (List<GPRS_DATA_RECORD>)ReBiaoPick.rxDataTable[id];
                        if (rxList != null)
                        {
                            for (int j = 0; j < rxList.Count; j++)
                            {
                                bRepeat = false;
                                recd = rxList[j];
                                DateTime rxdt = Convert.ToDateTime(recd.m_recv_date);
                                if (rxdt.AddSeconds(90) > DateTime.Now)
                                {
                                    if (old_rxList != null)
                                    {
                                        for (int k = 0; k < old_rxList.Count; k++)
                                        {
                                            if (old_rxList[k].m_recv_date == recd.m_recv_date)
                                            {
                                                //之前已经用过了
                                                bRepeat = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (!bRepeat)
                                    {
                                        bFind = true;
                                        break;
                                    }
                                }  
                            }
                        }
                        if (bFind)
                        {
                            ReBiaoData rb = ReBiaoData.MakeReBiaoData(recd.m_data_buf, 0);
                            if (rb != null)
                            {
                                dataList.Add(rb);
                                break;
                            }
                            else
                                return false;
                        }
                        DTURunInfo.WaitMS(1000);
                        wait--;
                    }
                    if (wait == 0)
                        return false;
                }                
            }
            catch (System.Exception ex)
            {
                return false;
            }   
        
            return true;
        }
    }
    public class stringfunc
    {
        public static string byteToHexStr(byte[] bytes, string spilt)
        {
            return byteToHexStr(bytes, 0, bytes.Length, spilt);
        }
        public static string byteToHexStr(byte[] bytes, int offset, int count, string spilt)
        {
            StringBuilder strB = new StringBuilder();
            if (bytes != null)
            {
                for (int i = 0; i < count; i++)
                {
                    strB.Append(bytes[offset + i].ToString("X2"));
                    strB.Append(spilt);
                }
            }
            return strB.ToString();
        }
    }
}
